package com.dfhc.tag;

import java.io.IOException;
import java.util.HashMap;
import java.util.Stack;

import javax.servlet.jsp.JspException;

import com.dfhc.ISystemConstant;
import com.dfhc.util.HttpClientUtil;
import com.dfhc.util.StringHelper;
/**
 * 页面布局模板标签 
 * @author longsebo
 */
public class FrameworkTemplateTag extends BaseTag {
	private static final long serialVersionUID = 1L;
	/**
	 * 模板对应url 
	 * 
	 */
	private String templateUrl;
    //创建线程局部变量堆栈，用来保存TEMPLATE_URL的堆栈对象
    private static final ThreadLocal templateStatck  = new ThreadLocal();
    /**
     * 获取当前线程中的Stack
     * @return Stack
     */
    public static Stack<String> currentStack()  {
    	Stack<String> s = (Stack<String>) templateStatck.get();
        // 如果Stack还没有打开，则新开一个Stack
        if (s  == null) {
            s = new Stack<String>();
            templateStatck.set(s);         //将新开的Session保存到线程局部变量中
       }
       return s;
    }
    /**
     * 清空当前线程的Stack 
     */
    public static void cleanStack() {
        //获取线程局部变量，并强制转换为Session类型
    	Stack<String> s = (Stack<String>) templateStatck.get();
    	//调用remove，防止内存泄漏
    	templateStatck.remove();
        templateStatck.set(null);
        if (s  != null){
            s.clear();
        }
    }

	public int doStartTag() throws JspException {
		if(StringHelper.isEmpty(templateUrl)){
			throw new JspException("模板url为空!");
		}
		//把页面模板url对应内容放入session ,以便后面Region从session提取加工
		HashMap<String, String> hashMap = new HashMap<String, String>();

		hashMap.put("Host", getRequest().getServerName() + ":"
				+ getRequest().getServerPort());
		hashMap.put("User-Agent",
				"Mozilla/5.0 (Windows NT 6.3; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
		hashMap.put("Accept",
				"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
		hashMap.put("Accept-Encoding", "gzip, deflate");
		hashMap.put("Accept-Language",
				"zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
		hashMap.put("Connection", "keep-alive");
		hashMap.put("Referer", getRequest().getScheme() + "://"
				+ getRequest().getServerName() + ":"
				+ getRequest().getServerPort()
				+ getRequest().getContextPath());
		hashMap.put("Cookie", "JSESSIONID="
				+ getRequest().getSession().getId());
		try {
			String info = HttpClientUtil.get(readRootUrl() + templateUrl, hashMap);
			Stack<String>   stack=currentStack();
			stack.push(info);
//			getRequest().getSession().setAttribute(ISystemConstant.TEMPLATE_URL, stack);
			
			
		} catch (IOException e) {
			throw new JspException(e);
		}
		
		return EVAL_BODY_BUFFERED;
	}

	public void doInitBody() throws JspException {
		super.doInitBody();
	}

	public int doAfterBody() throws JspException {
		return 6;
	}

	
	public int doEndTag() throws JspException {
		try {
			//从session取出已经替换完的页面
			Stack<String>   stack=currentStack();
			
			String  put=stack.pop();
			if(stack.isEmpty()){
//				 getRequest().getSession().removeAttribute(ISystemConstant.TEMPLATE_URL);
				cleanStack();
			}
			
			write(put);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return super.doEndTag();
	}

	public String getTemplateUrl() {
		return templateUrl;
	}

	public void setTemplateUrl(String templateUrl) {
		this.templateUrl = templateUrl;
	}		
}
